package com.t.bricks.bussiness.db.dao.flowwork.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.t.bricks.bussiness.db.dao.flowwork.FlowWorkNodeDao;
import com.t.bricks.bussiness.db.entity.flowwork.FlowWorkNode;
import com.t.bricks.bussiness.db.mapper.flowwork.FlowWorkMapper;
import com.t.bricks.bussiness.db.mapper.flowwork.FlowWorkNodeMapper;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class FlowWorkNodeDaoImpl implements FlowWorkNodeDao<FlowWorkNode> {

	@Autowired
	private FlowWorkMapper flowWorkMapper;
	
	@Autowired
	private FlowWorkNodeMapper flowWorkNodeMapper;

	@Value("${app.findPage.dLikeDate:配置文件中的当前库全文检索时间范围dLikeDate未设置}")
	private String sLikeDate;

	/**
	 * 修改
	 * 注意:更新成功后对象属性iVersion值将被+1
	 *     当对象的属性值为null时不更新字段
	 * @param flowWorkNode
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity editNode(FlowWorkNode flowWorkNode) {
		try {
			int i = flowWorkNodeMapper.editNode(flowWorkNode);
			if (i < 1) {
				Integer k = this.getiVersion(flowWorkNode.getuId());//如果更新操作的影响数小于1,则检查数据是否存在
				if (null == k) {
					return new MsgEmity(false, "修改数据失败，数据可能已经被删除！", 7001);// 无法获取版本号,说明数据已经不存在
				}
				
				if (!flowWorkNode.getiVersion().equals(k)) {
					return new MsgEmity(false, "修改数据失败，系统中的数据可能已经被更新！", 7002);
				}

				Log.error("修改未能得到预期影响值(预期大于0):", i);
				return new MsgEmity(false, "修改数据失败！", 7003);
			}

			flowWorkNode.setiVersion(flowWorkNode.getiVersion() +1);
		} catch (Exception e) {
			Log.error("修改操作发生异常:", e.getMessage());
			return new MsgEmity(false, "修改失败！", 7004);
		}
		
		return new MsgEmity(true, "修改成功！", flowWorkNode);
	}

	/**
	 * 审批节点
	 * @param flowWorkNode 当前节点
	 * @return
	 */
	@Override
	@Transactional
	public MsgEmity auditing(FlowWorkNode flowWorkNode) {
		try {
			int i = flowWorkNodeMapper.auditing(flowWorkNode);
			if (i < 1) {
				Integer k = this.getiVersion(flowWorkNode.getuId());//如果更新操作的影响数小于1,则检查数据是否存在
				if (null == k) {
					return new MsgEmity(false, "修改数据失败，数据可能已经被删除！", 7001);// 无法获取版本号,说明数据已经不存在
				}
				
				if (!flowWorkNode.getiVersion().equals(k)) {
					return new MsgEmity(false, "修改数据失败，系统中的数据可能已经被更新！", 7002);
				}

				Log.error("修改未能得到预期影响值(预期大于0):", i);
				return new MsgEmity(false, "修改数据失败！", 7003);
			}
			
			if (null == flowWorkNode.getsNext()) {//如果有下一个节点要操作,则修改下一个节点的状态
				FlowWorkNode flowWorkNodeNext = new FlowWorkNode();
				flowWorkNodeNext.setuId(flowWorkNode.getsNext());
				flowWorkNodeNext.setiState(2);//状态(枚举,1:待执行;2:执行中;3:审核完毕;4:任务执行失败;5:已完结)
				i = flowWorkNodeMapper.editNode(flowWorkNodeNext);
				if (i < 1) {
					Log.error("修改下一个节点状态未能得到预期影响值(预期1):", i);
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
					return new MsgEmity(false, "修改下一个节点状态未能得到预期影响值！", 7004);
				}
			}
			
			if (1 != flowWorkNode.getiChangeSuccessState().intValue()) {//影响整个流程成功状态(布尔值,1:是)
				flowWorkNode.setiVersion(flowWorkNode.getiVersion() +1);
				return new MsgEmity(true, "修改成功！", flowWorkNode);
			}
			
			i = flowWorkMapper.changeStateByNode(flowWorkNode.getsWorkId(), flowWorkNode.getiSuccess());
			if (i < 1) {
				Log.error("修改工作流状态未能得到预期影响值(预期大于0):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "修改工作流状态失败！", 7005);
			}
		} catch (Exception e) {
			Log.error("修改操作发生异常:", e.getMessage());
			return new MsgEmity(false, "修改失败！", 7006);
		}
		
		return new MsgEmity(true, "修改成功！", flowWorkNode);
	}
	
	/**
	 * 结束节点
	 * @param flowWorkNode 当前节点
	 * @return
	 */
	@Override
	@Transactional
	public MsgEmity overNode(FlowWorkNode flowWorkNode) {
		try {
			int i = flowWorkNodeMapper.over(flowWorkNode);
			if (i < 1) {
				Integer k = this.getiVersion(flowWorkNode.getuId());//如果更新操作的影响数小于1,则检查数据是否存在
				if (null == k) {
					return new MsgEmity(false, "修改数据失败，数据可能已经被删除！", 7001);// 无法获取版本号,说明数据已经不存在
				}
				
				if (!flowWorkNode.getiVersion().equals(k)) {
					return new MsgEmity(false, "修改数据失败，系统中的数据可能已经被更新！", 7002);
				}

				Log.error("修改未能得到预期影响值(预期大于0):", i);
				return new MsgEmity(false, "修改数据失败！", 7003);
			}
			
			if (null == flowWorkNode.getsNext()) {//如果有下一个节点要操作,则修改下一个节点的状态
				FlowWorkNode flowWorkNodeNext = new FlowWorkNode();
				flowWorkNodeNext.setuId(flowWorkNode.getsNext());
				flowWorkNodeNext.setiState(2);//状态(枚举,1:待执行;2:执行中;3:审核完毕;4:任务执行失败;5:已完结)
				i = flowWorkNodeMapper.editNode(flowWorkNodeNext);
				if (i < 1) {
					Log.error("修改下一个节点状态未能得到预期影响值(预期1):", i);
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
					return new MsgEmity(false, "修改下一个节点状态未能得到预期影响值！", 7004);
				}
			}
			
			if (1 != flowWorkNode.getiChangeSuccessState().intValue()) {//影响整个流程成功状态(布尔值,1:是)
				flowWorkNode.setiVersion(flowWorkNode.getiVersion() +1);
				return new MsgEmity(true, "修改成功！", flowWorkNode);
			}
			
			i = flowWorkMapper.changeStateByNode(flowWorkNode.getsWorkId(), flowWorkNode.getiSuccess());
			if (i < 1) {
				Log.error("修改工作流状态未能得到预期影响值(预期大于0):", i);
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
				return new MsgEmity(false, "修改工作流状态失败！", 7005);
			}
		} catch (Exception e) {
			Log.error("修改操作发生异常:", e.getMessage());
			return new MsgEmity(false, "修改失败！", 7006);
		}
		
		return new MsgEmity(true, "修改成功！", flowWorkNode);
	}
	
	/**
	 * 查找发起者节点
	 * @param sWorkId
	 * @return
	 */
	@Override
	public MsgEmity findByType2(String sWorkId) {
		FlowWorkNode flowWorkNode = null;
		try {
			flowWorkNode = flowWorkNodeMapper.findByType2(sWorkId);
			if (null == flowWorkNode) {
				return new MsgEmity(false, "数据不存在！", 7001);
			}
		} catch (Exception e) {
			Log.error("查找发起者节点发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败！", 7002);
		}
		
		return new MsgEmity(true, "查找发起者节点成功！", flowWorkNode);
	}
	
	/**
	 * 查找所有审核完毕待执行任务的节点
	 * @return
	 */
	@Override
	public MsgEmity findByType3() {
		List<FlowWorkNode> list = null;
		try {
			list = flowWorkNodeMapper.findByType3();
			if (null == list) {
				Log.error("查找所有审核完毕待执行任务的节点发生异常,执行的返回值为:null,预期值不应为null");
				return new MsgEmity(false, "查找所有审核完毕待执行任务的节点操作失败！", 7001);
			}
		} catch (Exception e) {
			Log.error("查找所有审核完毕待执行任务的节点发生异常:", e.getMessage());
			return new MsgEmity(false, "查找所有审核完毕待执行任务的节点失败，请查看异常日志！", 7002);
		}
		
		return new MsgEmity(true, "查找所有审核完毕待执行任务的节点成功！", list);
	}
	
	/**
	 * 查找所有审核完毕待执行任务的节点数量
	 * @return
	 */
	@Override
	public int findCountByType3() {
		try {
			return flowWorkNodeMapper.findCountByType3();
		} catch (Exception e) {
			Log.error("查找所有审核完毕待执行任务的节点发生数量异常:", e.getMessage());
			return 0;
		}
	}
	
	/**
	 * 更新任务执行信息
	 * @param flowWorkNode
	 * @return
	 */
	@Override
	public int editApiResult(FlowWorkNode flowWorkNode) {
		try {
			return flowWorkNodeMapper.editApiResult(flowWorkNode);
		} catch (Exception e) {
			Log.error("更新任务执行信息发生数量异常:", e.getMessage());
			return 0;
		}
	}

}
